#!/usr/bin/env python3
# -*- coding: utf-8 -*-


from flask import Blueprint, request
from .lib import *
from database import con
from scheme import *
scheme = Blueprint('scheme', __name__)

@scheme.route('/scheme/test')
def test(id=1,date="20210101"):
    return {
            "code":-1,
            "msg":"text"
        }
# 获取策略推荐股票
@scheme.route('/scheme/rec',methods=['GET', 'POST'])
def recommend(id=1,date="20210101"):
    
    cur = con.cursor()
    try:
        
        data = request_parse(request)
        scheme_id = data["id"]
        date = data["date"]
        sql = ("""
        SELECT t2.name as name from scheme t1
        LEFT JOIN scheme_template t2 ON t1.template = t2.id
        WHERE t1.id = %s
        """ % scheme_id)
    
        cur.execute(sql)
        result = cur.fetchone()
        clsstr = result["name"]
        schemeObj = eval(("%s(id=%s)" % (clsstr,scheme_id)))
        etfbuy = schemeObj.befororder_buy(date = date,type=1)
        storebuy = schemeObj.befororder_buy(date = date,type=0)

        etfsell = schemeObj.befororder_sell(date = date,type=1)
        storesell = schemeObj.befororder_sell(date = date,type=0)


        buydata = etfbuy+storebuy
        selldata = etfsell+storesell
        for item in buydata:
            item["dir"] = 0
        for item in selldata:
            item["dir"] = 1
        return {
            "code":0,
            "data":buydata+selldata
        }
    except  Exception as e:
        print(e)
        return {
            "code":-1,
            "msg":str(e)
        }
    finally:
        cur.close()

# 获取策略推荐股票
@scheme.route('/scheme/beforeOrder',methods=['GET', 'POST'])
def beforeOrder(id=1,date="20210101"):
    
    cur = con.cursor()
    try:
        
        data = request_parse(request)
        scheme_id = data["id"]
        date = data["date"]
        sql = ("""
        SELECT t2.name as name from scheme t1
        LEFT JOIN scheme_template t2 ON t1.template = t2.id
        WHERE t1.id = %s
        """ % scheme_id)
    
        cur.execute(sql)
        result = cur.fetchone()
        clsstr = result["name"]
        schemeObj = eval(("%s(id=%s)" % (clsstr,scheme_id)))
        etfbuy0 = schemeObj.befororder_buy(date = date,type=1)
        etfbuy = schemeObj.fitter_befororder_buy(date,type=1,result=etfbuy0)
        storebuy0 = schemeObj.befororder_buy(date = date,type=0)
        storebuy = schemeObj.fitter_befororder_buy(date,type=1,result=storebuy0)
        etfsell0 = schemeObj.befororder_sell(date = date,type=1)
        etfsell = schemeObj.fitterbefororder_sell(date = date,type=1,result=etfsell0)
        storesell0 = schemeObj.befororder_sell(date = date,type=0)
        storesell = schemeObj.fitterbefororder_sell(date = date,type=0,result=storesell0)
   
        buydata = etfbuy+storebuy
        selldata = etfsell+storesell
        for item in buydata:
            item["dir"] = 0
        for item in selldata:
            item["dir"] = 1
        return {
            "code":0,
            "data":buydata+selldata
        }
    except  Exception as e:
        print(e)
        return {
            "code":-1,
            "msg":str(e)
        }
    finally:
        cur.close()
  


# 给策略添加code
@scheme.route('/scheme/update',methods=['GET', 'POST'])
def addCode():
    cur = con.cursor()
    try:
        cur = con.cursor()
        data = request_parse(request)
        scheme_id = data["id"]
        sql = data["sql"]
        cur.execute(sql)
        result={"count":cur.rowcount}
        con.commit()
        return {
            "code":0,
            "data":result
        }
    except  Exception as e:
        print(e)
        return {
            "code":-1,
            "msg":str(e)
        }
    finally:
        cur.close()


# 给策略添加code
@scheme.route('/scheme/exit',methods=['GET', 'POST'])
def exit():
    
    try:
        cur = con.cursor()
        data = request_parse(request)
        scheme_id = data["id"]
        sql = ("""
        SELECT t2.name as name from scheme t1
        LEFT JOIN scheme_template t2 ON t1.template = t2.id
        WHERE t1.id = '%s'
        """ % scheme_id)
    
        cur.execute(sql)
        result = cur.fetchone()
        clsstr = result["name"]
        schemeObj = eval(("%s(id=%s)" % (clsstr,scheme_id)))
        ex = Exchange(schemeObj)
        ex.main()
        return {
            "code":0,
            "data":ex.jobid
        }
    except  Exception as e:
        import traceback
        traceback.print_stack()
        print(e)
        return {
            "code":-1,
            "msg":str(e)
        }
        
   
